{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### The power of `Langchain 🦜` "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install langchain\n",
    "#!pip install openai\n",
    "#!pip install google-api-python-client\n",
    "#!pip install wikipedia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import openai\n",
    "from langchain.chat_models import ChatOpenAI\n",
    "from langchain.agents import load_tools, initialize_agent\n",
    "from langchain.memory import ConversationBufferMemory\n",
    "from IPython.display import Markdown"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Set environment variables\n",
    "os.environ['GOOGLE_API_KEY'] = '...'\n",
    "os.environ['OPENAI_API_KEY'] = '...'\n",
    "os.environ['GOOGLE_CSE_ID'] = '...'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [],
   "source": [
    "# A conversation buffer (memory) & import llm of choice\n",
    "memory = ConversationBufferMemory()\n",
    "llm = ChatOpenAI()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Provide access to a list of tools that the agents will use\n",
    "tools = load_tools(['wikipedia', 'google-search', 'llm-math'], llm=llm)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialise the agents & make all the tools and llm available to it\n",
    "agent = initialize_agent(tools, llm, agent='zero-shot-react-description', verbose=True, memory=memory)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [],
   "source": [
    "# provide a prompt and you are done!\n",
    "agent.run(\"Find the number of IPL titles won by MS Dhoni & find it's cube root\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "\n",
      "\n",
      "\u001b[1m> Entering new AgentExecutor chain...\u001b[0m\n",
      "\u001b[32;1m\u001b[1;3mI need to find information about IPL titles won by Virat Kohli. Since this is a sports-related question, I should use Google Search.\n",
      "Action: Google Search\n",
      "Action Input: \"Virat Kohli IPL titles\"\u001b[0m\n",
      "Observation: \u001b[33;1m\u001b[1;3mVirat Kohli, the captain of the Royal Challengers Bangalore (RCB) team in the Indian Premier League (IPL), has not won the IPL title so far. Virat Kohli is an Indian international cricketer and the former captain of the Indian national cricket team who plays as a right-handed batsman for Royal ... Jun 27, 2021 ... As India lost the World Test Championship final to New Zealand by eight wickets, it marked the third instance that under Virat Kohli, ... May 22, 2022 ... For all his exploits for India, he has never won the IPL title since joining Bangalore before the inaugural contest in 2008, including as ... Apr 23, 2023 ... It's a welcome reminder that Kohli is still chasing an IPL title, which if successful would be a deserved triumph for the most important player ... Mar 30, 2023 ... Sanjay Manjrekar believes Virat Kohli and Royal Challengers ... their first IPL title in the upcoming season of the Indian Premier League. May 14, 2023 ... Talisman Virat Kohli might be cricket's biggest superstar, but his performances in ... and somehow are still chasing an elusive IPL title. May 10, 2023 ... Virat Kohli's dream of winning the Indian Premier League trophy is almost over after RCB's defeat to Mumbai Indians. Oct 12, 2021 ... It was to be Virat Kohli's last match as captain of Royal Challengers Bangalore. Or of any IPL team. RCB put up a sub par performance ... 3 days ago ... Virat Kohli has dominated the Indian Premier League (IPL) as well as ... reach the playoffs as the wait for a maiden IPL title continues.\u001b[0m\n",
      "Thought:\u001b[32;1m\u001b[1;3mBased on the observation, it seems that Virat Kohli has not won any IPL titles yet.\n",
      "Final Answer: Virat Kohli has not won any IPL titles.\u001b[0m\n",
      "\n",
      "\u001b[1m> Finished chain.\u001b[0m\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "'Virat Kohli has not won any IPL titles.'"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "agent.run(\"What is the number of IPL titles won by Virat Kohli?\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [],
   "source": [
    "#!pip install google-api-python-client\n",
    "# !pip install wikipedia"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "env_twitter",
   "language": "python",
   "name": "env_twitter"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.6"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
